% read imgs
negImgs = imageDatastore('neg');
posImgs = imageDatastore('pos');
Error using imageDatastore (line 85)
Folder 'new' does not have any files or is empty.

Use 'IncludeSubfolders' to include files in all subfolders.
% Create a cascade detector object.
faceDetector = vision.CascadeObjectDetector();
noseDetector = vision.CascadeObjectDetector('Nose', 'UseROI', true);
bboxImgs=[];
for i = 1:59
img = readimage(posImgs, i);
bbox = step(faceDetector, img);
% noseBBox = step(noseDetector, img, bbox(1,:));
videoOut = insertObjectAnnotation(img,'rectangle',bbox,'Face');
figure, imshow(videoOut), title('Detected face');
bboxImgs = [bboxImgs; bbox];
end
save('step1.mat');

add address and bbox into variable

load('step1.mat');
posInstance = [];
pos = [];
negInstance = [];
for i = 1:59
% bboxImgs(i,:)
% posImgs.Files(i)
pos = [posImgs.Files(i), bboxImgs(i,:)];
posInstance = [posInstance; pos];
%negInstance = [negInstance; negImgs.Files(i)];
end
save('step2.mat');

Training

load('step2.mat');
filenames = {'imageFilename', 'objectBoundingBoxes'};
posInstance = cell2struct(posInstance, filenames, 2);
negFolder='C:\Users\chaofanz\Desktop\utasLearning\412ComputerVision\week10 face detection\neg';
% Train a cascade object detector called 'stopSignDetector.xml' using HOG features.
trainCascadeObjectDetector('kelDetector.xml',posInstance, ...
negFolder,'FalseAlarmRate',0.1,'NumCascadeStages',5,'FeatureType', 'Haar');
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Automatically setting ObjectTrainingSize to [ 32, 32 ] Using at most 11 of 11 positive samples per stage Using at most 22 negative samples per stage --cascadeParams-- Training stage 1 of 5 [..
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
......................................................................] Used 11 positive and 22 negative samples Time to train stage 1: 0 seconds Training stage 2 of 5 [........................................................................] Used 11 positive and 22 negative samples Time to train stage 2: 0 seconds Training stage 3 of 5 [........................................................................] Used 11 positive and 22 negative samples Time to train stage 3: 0 seconds Training stage 4 of 5 [..
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
......................................................................] Used 11 positive and 22 negative samples Time to train stage 4: 6 seconds Training stage 5 of 5 [..
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
..................................................
Warning:
Unable to generate a sufficient number of negative samples for this stage.
Consider reducing the number of stages, reducing the false alarm rate
or adding more negative images.
Cannot find enough samples for training. Training will halt and return cascade detector with 4 stages Time to train stage 5: 12 seconds Training complete
save('step3.mat');

Testing

% load('step3.mat');
% Use the newly trained classifier to detect a stop sign in an image.
detector = vision.CascadeObjectDetector('kelDetector.xml');
% load img
testImgs = imageDatastore('test');
for i=1:10
img = readimage(testImgs, i);
% Detect a stop sign.
bbox = step(detector,img);
% Insert bounding box rectangles and return the marked image.
detectedImg = insertObjectAnnotation(img,'rectangle',bbox,'stop sign');
% Display the detected stop sign.
figure; imshow(detectedImg);
end
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
Warning: JPEG library error (8 bit), "Invalid SOS parameters for sequential JPEG".
Warning: Image is too big to fit on screen; displaying at 67%
% the small box can be filter by setting the range of bbox easily
% the most important is to get the big box to recognise the right person rather than the wrong person;
% some small box can be filter by the distance of the target